﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Reflection;

namespace HeerCards.Helper
{
    public class CardImageHelper
    {
        //private List<HCCard> Cards;
        private List<string> Paths;

        private List<string> Descs;

        public CardImageHelper(List<PoolGetInfo> getInfos, string path)
        {
            Paths = new List<string>();
            Descs = new List<string>();

            foreach (var item in getInfos)
            {
                Paths.Add(path + item.Card.LocalImagePath);
                Descs.Add(item.Card.Name);
            }
        }

        public Bitmap createImage(string backimg = null)
        {
            Dictionary<string, int> dic = new Dictionary<string, int>();
            foreach (var item in Paths.ToArray())
            {
                if (!dic.ContainsKey(item))
                {
                    dic.Add(item, 0);
                }
                dic[item]++;
            }
            var img = new Bitmap(1000, 500);
            Graphics g = Graphics.FromImage(img);
            if (backimg != null)
            {
                g.DrawImage(Image.FromFile(backimg), 0, 0, 1000, 500);
            }
            else
            {
                g.FillRectangle(Brushes.LightBlue, 0, 0, 1000, 500);//画背景
            }
            if (Paths.Count <= 10)
            {
            for (int i2 = 0; i2 < 10; i2++)
            {
                var item = Paths[i2];
                var desc = Descs[i2];
                var x = i2 < 5 ? 200 * i2  : 200 * (i2 - 5) ;
                var y = i2< 5 ? 0 : 250 ;

                g.DrawImage(Image.FromFile(item), x + 10, y + 10, 200, 200);//画图片
                g.DrawString(
                   desc.ToString(),
                    new Font("微软雅黑", 16, FontStyle.Bold),
                    Brushes.White
                    , x
                    , y + 220
                ) ;
            }

            }
            else
            {
            int i = 0;
            foreach (var item in dic)
            {
                if (dic.Keys.Count - 1 < i)
                {
                    return img;
                    break;
                }
                if (!File.Exists(Paths[i]))
                {
                    throw new Exception("找不到图片" + item.Key);
                }
                var x = i < 5 ? 200 * i : 200 * (i - 5);
                var y = i < 5 ? 0 : 250;
                //画边框
                g.DrawImage(Image.FromStream(
                    Assembly.GetExecutingAssembly()
                    //"github.heerheer.card2.Code.Resources.Border.png"
                    .GetManifestResourceStream(
                    "HeerCards.Resources.Border.png"
                    )
                    ), x, y, 200, 250);
                g.DrawImage(Image.FromFile(item.Key), x + 20, y + 20, 160, 160);//画图片
                g.DrawString(
                    "x" + item.Value.ToString(),
                    new Font("微软雅黑", 16, FontStyle.Bold),
                    Brushes.White,
                    new Rectangle(x + 60, y + 200, 80, 20),// 60,200 =>140,220
                    new StringFormat() { LineAlignment = StringAlignment.Center }

                );
                i++;
            }

            }
            //几次版
            //十次一下
            g.Dispose();
            return img;
        }
    }
}